package com.rj.ability.leqi.adapter.handler.aisino;

import cn.hutool.core.date.DateUtil;
import com.rj.ability.leqi.adapter.domain.aisino.*;
import com.rj.ability.leqi.adapter.handler.AbstractQdhzqrdsqHandler;
import com.rj.ability.leqi.common.BizSuccessCommonRes;
import com.rj.ability.leqi.req.QdhzqrdmxData;
import com.rj.ability.leqi.req.QdhzqrdsqReq;
import com.rj.ability.leqi.resp.QdhzqrsqRes;
import com.rj.util.MapstructUtils;
import io.github.linpeilie.Converter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 红字信息单处理
 *
 * @author YFX_RANLUBO
 * @date 2024/09/30 16:21
 **/
public class QdhzqrdsqHandler extends AbstractQdhzqrdsqHandler<AisinoQdhzqrdsqReq, AisinoQdhzqrdsqRes> {

    @Override
    protected QdhzqrdsqReq convert(AisinoQdhzqrdsqReq req) {
        //参数校验 TODO
        String invoiceTypeCode = req.getInvoiceTypeCode();
        String fppz = DictConvert.get(DictConvert.FPPZ, invoiceTypeCode);
        //包信息
        RedLetterInfoForm redLetterInfoForm = req.getRedLetterInfoForm();
        //红字信息单内容
        AisinoQdhzqrdsq group = redLetterInfoForm.getGroup();
        //发票明细数据
        InvoiceProductList product = group.getProductList();
        List<InvoiceProduct> list = product.getGroup();
        //类型转换
        QdhzqrdsqReq leqReq = MapstructUtils.convert(group, QdhzqrdsqReq.class);
        leqReq.setLzkprq(DateUtil.format(group.getIssueDate(), "yyyy-MM-dd HH:mm:ss"));
        List<QdhzqrdmxData> qdmxs = convert(list);
        leqReq.setHzqrdmxList(qdmxs);
        leqReq.setLzfppzDm(fppz);
        leqReq.setSfzzfpbz("N");
        leqReq.setLrfsf(DictConvert.get(DictConvert.LRFSF, leqReq.getLrfsf()));
        leqReq.setChyyDm(DictConvert.get(DictConvert.CHYYDM, leqReq.getChyyDm()));
        leqReq.setLzfpTdyslxDm(DictConvert.get(DictConvert.TDYS, leqReq.getLzfpTdyslxDm()));
        super.setBaseUserInfo(leqReq.getXsfnsrsbh());
        return null;
    }

    @Override
    protected AisinoQdhzqrdsqRes convert(QdhzqrsqRes lqRes) {
        return null;
    }

    private List<QdhzqrdmxData> convert(List<InvoiceProduct> group) {
        return group.stream().map(invoiceProduct -> {
            invoiceProduct.check();
            QdhzqrdmxData mx = MapstructUtils.convert(invoiceProduct, QdhzqrdmxData.class);
            mx.setSl1(invoiceProduct.getSlv());
            mx.setLzmxxh(invoiceProduct.getDylzfpmxxh());
            mx.setXh(invoiceProduct.getMxxh());
            mx.setHwhyslwfwmc(mx.getXmmc());
            String[] xmJcAndXmmc = mx.getXmmc().split("\\*");
            mx.setSpfwjc(xmJcAndXmmc[1]);
            mx.setXmmc(xmJcAndXmmc[2]);
            mx.setFpspdj(invoiceProduct.getDj());
            mx.setFpspsl(invoiceProduct.getSl());
            mx.setJe(invoiceProduct.getAmount());
            return mx;
        }).collect(Collectors.toList());

    }
}
